package test.teacher.common.filter;

/**
 * @date 2018/5/18  15:13
 */
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CharSetFilter implements Filter {
    private  String  encode;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("filter启动了");
       encode = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter正在过滤字符问题");
        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
        HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
        servletRequest1.setCharacterEncoding(encode);
        servletResponse1.setContentType("text/html;charset="+encode);
        System.out.println("response文本格式已被设置成utf-8");
        filterChain.doFilter(servletRequest1,servletResponse1);
    }

    @Override
    public void destroy() {
        System.out.println("filter销毁了");
    }
}
